#include <bits/stdc++.h>

#define in read()
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using vec = vector < int >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 610;

int dis[N][N], n, m1, m2, low[N], dfn[N], tim, stk[N], top, sccnum, ans, bel[N];
vec G[N], scc[N];
bool vis[N];

inline void link(int x, int y, int w) { G[x].pb(y); dis[x][y] = min(dis[x][y], w); }

void tarjan(int x) {
	low[x] = dfn[x] = ++tim; vis[x] = true; stk[++top] = x;
	for(auto y : G[x])
		if(!low[y]) tarjan(y), low[x] = min(low[x], low[y]);
		else if(vis[y]) low[x] = min(low[x], dfn[y]);
	if(low[x] == dfn[x]) {
		int y; ++sccnum;
		do {
			y = stk[top--]; vis[y] = false; scc[sccnum].pb(y); bel[y] = sccnum;
		} while(y ^ x);
	}
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in, m1 = in, m2 = in;
	memset(dis, 0x3f, sizeof dis); rep(i, 1, n) dis[i][i] = 0;
	rep(i, 1, m1) {
		int a = in, b = in;
		link(a, b, -1); link(b, a, 1);
	}
	rep(i, 1, m2) {
		int a = in, b = in;
		link(a, b, 0);
	}
	rep(i, 1, n) if(!dfn[i]) tarjan(i);
	rep(k, 1, n) {
		rep(i, 1, n)
			if(bel[i] == bel[k]) {
				rep(j, 1, n)
					if(bel[j] == bel[k]) {
						dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
					}
			}
	}
	rep(i, 1, n) if(dis[i][i]) return puts("NIE"), 0;
	rep(i, 1, sccnum) {
		int mx = 0;
		for(auto x : scc[i])
			for(auto y : scc[i])
				mx = max(mx, dis[x][y]);
		ans += mx + 1;
	}
	cout << ans << endl; return 0;
}
